//
//  ViewController.m
//  TestGCD
//
//  Created by StormVCC on 2017/11/3.
//  Copyright © 2017年 StormVCC. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic ,strong) dispatch_queue_t serialQueue;
@property (nonatomic ,strong) dispatch_queue_t parallelismQueue;
@property (nonatomic ,strong) IBOutlet UITextField *tf;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpQueue];
    
}

- (void)setUpQueue {
    _serialQueue = dispatch_queue_create("sadfas", DISPATCH_QUEUE_SERIAL);
    _parallelismQueue = dispatch_queue_create("标识符", DISPATCH_QUEUE_CONCURRENT);
}

- (IBAction)printXX {
    int i = [_tf.text intValue];
    [self hourglass:i];
    [self quadrilateral:i];
}

- (IBAction)algorithmEvent:(id)sender {
    
}

/**
 四边形

 @param i 基数
 */
- (void)quadrilateral:(int)i {
    if (i > 0) {
        NSMutableArray *strArr = [NSMutableArray arrayWithArray:@[]];
        NSLog(@"\n");
        int num = 0;
        int divisor = 1;
        int maxDivisor = 0;
        int index = 0;
        BOOL isBol = YES;
        do {
            divisor = divisor + (i * 2);
         
            
            
            
            index ++;
            isBol = NO;
        } while (isBol);
        
    }
}

 /*
        *
       ***
      *****
     *******
    *********
     *******
      *****
       ***
        *
 */

/**
 沙漏形状

 @param i 基数
 */
- (void)hourglass:(int)i {
    if (i > 0) {
        NSMutableArray *strArr = [NSMutableArray arrayWithArray:@[]];
        NSLog(@"\n");
        int b = i - 1;
        //除去中间的一个星后，分到沙漏两头的基数
        int modC = b / 2;
        int num = 0;
        BOOL isYes = YES;
        int divisor = 3;
        int surplus = 0;
        int modTemp = modC;
        //循环判断除去中间的一个星，剩下的能够摆几行
        while (isYes) {
            if ((modTemp -= divisor) >= 0) {
                num ++;
                divisor += 2;
            } else {
                surplus = divisor + modTemp;
                if (modTemp < 0) {
                    divisor -=2;
                }
                isYes = NO;
            }
        }
        int newNum = num * 2 + 1;
        int maxDivisor = divisor;
        surplus *= 2;
        
        /** 沙漏状 */
        for (int i = 1; i <= newNum;  i ++) {
            int spaceNum = (maxDivisor - divisor) / 2;
            for (int i = 1; i <= maxDivisor ; i++) {
                if (i <= spaceNum) {
                    [strArr addObject:@" "];
                } else if (i <= divisor + spaceNum) {
                    [strArr addObject:@"*"];
                } else {
                    [strArr addObject:@" "];
                }
            }
            if (i - num <= 0) {
                //上半部分
                NSLog(@"%@",[strArr componentsJoinedByString:@""]);
                [strArr removeAllObjects];
                divisor -= 2;
            } else if (i == num + 1) {
                //中间部分
                NSLog(@"%@",[strArr componentsJoinedByString:@""]);
                [strArr removeAllObjects];
                divisor = 3;
            } else {
                //下半部分的第一行
                NSLog(@"%@",[strArr componentsJoinedByString:@""]);
                [strArr removeAllObjects];
                divisor += 2;
            }
        }
        NSLog(@"%zd",surplus);
    }
}

//同步串行
- (IBAction)syncSerial:(id)sender {
    [self sycn:_serialQueue];
}

//同步并行
- (IBAction)syncConcurrent:(id)sender {
    [self sycn:_parallelismQueue];
}

//异步串行
- (IBAction)asyncSerial:(id)sender {
    [self asycn:_serialQueue];
}

//异步并行
- (IBAction)asyncConcurrent:(id)sender {
    [self asycn:_parallelismQueue];
}

//栅栏
- (IBAction)palisade:(id)sender {
    dispatch_time_t time = getDispatchTimeByData([NSDate date]);
    NSLog(@"milestone:%zd", time);
}

dispatch_time_t getDispatchTimeByData(NSDate *date) {
    NSTimeInterval interval;
    double second , subsecond;
    struct timespec time;
    dispatch_time_t milestone;
    
    interval = [date timeIntervalSince1970];
    subsecond = modf(interval, &second);
    time.tv_sec = second;
    time.tv_nsec = subsecond * NSEC_PER_SEC;
    milestone = dispatch_walltime(&time, 0);
    return milestone;
}

- (void)barrier {
    [self asycn:_parallelismQueue];
    
}

//异步
- (void)asycn:(dispatch_queue_t)queue {
    NSLog(@"asyc ---start---");
    
    //使用异步函数封装三个任务
    dispatch_async(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    
    NSLog(@"---end---");
}

//同步
- (void)sycn:(dispatch_queue_t)queue {
    
    NSLog(@"sycn ---start---");
    
    //使用异步函数封装三个任务
    dispatch_sync(queue, ^{
        NSLog(@"任务1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"任务3---%@", [NSThread currentThread]);
    });
    
    NSLog(@"---end---");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
